package com.android.kotlincoroutinemvvmdemo
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityGlideCaseBinding
import com.android.kotlincoroutinemvvmdemo.glideoptions.GlideApp
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

class GlideCaseActivity : BaseActivity<ActivityGlideCaseBinding>() {

    override fun getLayoutId() = R.layout.activity_glide_case

    override fun init(savedInstanceState: Bundle?) {

//        Glide.with(this@GlideCaseActivity)
//            .load(URL)
//            .into(mBinding.ivAvatar)

        val bmpListener = object : RequestListener<Bitmap> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Bitmap>,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
            override fun onResourceReady(
                resource: Bitmap,
                model: Any,
                target: Target<Bitmap>?,
                dataSource: DataSource,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
        }

        val drawableListener = object : RequestListener<Drawable> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
            override fun onResourceReady(
                resource: Drawable,
                model: Any,
                target: Target<Drawable>?,
                dataSource: DataSource,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }
        }

        GlideApp.with(this@GlideCaseActivity)
            //.asBitmap()//与RequestListener<R>类型进行绑定
            //.createBitmap()
            .createDrawable()
            //.asDrawable()
            //.asGif()
            //.asFile()
            .load(URL)
            .applyAvatar(144 * 3)
            //淡入淡出处理
            //.transition(DrawableTransitionOptions.withCrossFade())
            .listener(drawableListener)
            .into(mBinding.ivAvatar)

          //异步获取相关的图片并转换成Drawable、Bitmap之后再在主线程中绑定View
//        launch {
//            val drawable = withContext(Dispatchers.IO) {
//                GlideApp.with(this@GlideCaseActivity)
//                    .asDrawable()
//                    .load(URL)
//                    .applyAvatar(144 * 3)
//                    .submit()
//                    .get()
//            }
//            mBinding.ivAvatar.setImageDrawable(drawable)
//        }

        val mWidthHeight = resources.getDimension(R.dimen.dp_200).toInt()
        GlideApp
            .with(this)
            .asDrawable()
            .load(URL)
            .override(mWidthHeight, mWidthHeight)
            .into(mBinding.ivAvatar2)
    }

    companion object {
        const val URL =
            "http://b247.photo.store.qq.com/psb?/V11ZojBI312o2K/63aY8a4M5quhi.78*krOo7k3Gu3cknuclBJHS3g1fpc!/b/dDXWPZMlBgAA"
    }
}